package com.nami.store.biz.conver.application;

import cn.hutool.core.collection.CollUtil;
import com.nami.store.api.request.conversation.ConverAddReq;
import com.nami.store.api.request.conversation.ConverC2CUpdRequest;
import com.nami.store.api.request.conversation.ConverItem;
import com.nami.store.api.request.conversation.ConverPageReq;
import com.nami.store.api.response.conversation.ConversationDTO;
import com.nami.store.api.response.conversation.ConverDetailVO;
import com.nami.store.biz.conver.convert.ConverConvert;
import com.nami.store.biz.conver.service.ConverDomainService;
import com.nami.store.biz.conver.service.PrivateConverDomainService;
import com.nami.store.domain.ConversationDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ConverAppServiceImpl implements ConverAppService {

    private final ConverDomainService converDomainService;

    private final PrivateConverDomainService privateConverDomainService;

    @Override
    public List<ConversationDTO> getConversFromCacheOrDb(String appKey, List<String> userIds, String targetId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(targetId) || CollUtil.isEmpty(userIds)) {
            return null;
        }
        return converDomainService.getConversFromCacheOrDB(appKey, userIds, targetId);
    }

    @Override
    public ConversationDTO getConverFromCacheOrDB(String appKey, String userId, String targetId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(userId) || StringUtils.isBlank(targetId)) {
            return null;
        }

        return converDomainService.getConverFromCacheOrDB(appKey, userId, targetId);
    }

    @Override
    public boolean batchSave(List<ConverAddReq> requests) {
        if (CollUtil.isEmpty(requests)) {
            return Boolean.FALSE;
        }

        List<ConversationDO> convers = ConverConvert.INSTANCE.convertList3(requests);
        return converDomainService.batchSave(convers);
    }

    @Override
    public boolean insertOrUpd(ConverItem converItem) {
        ConversationDO conver = ConverConvert.INSTANCE.convert2(converItem);
        if (conver == null) {
            log.warn("[conver] conver is null.");
            return Boolean.FALSE;
        }
        return converDomainService.insertOrUpd(conver);
    }

    @Override
    public boolean updateGrpConvers(ConversationDO conversationDO) {
        return false;
    }

    @Override
    public boolean batchUpdConvers(List<ConverItem> itemList) {
        if (CollUtil.isEmpty(itemList)) {
            return Boolean.FALSE;
        }
        return converDomainService.batchUpdConvers(itemList);
    }

    @Override
    public boolean updPrivateChatConver(ConverC2CUpdRequest request) {
        if (request == null) {
            return Boolean.FALSE;
        }
        return privateConverDomainService.updPrivateChatConver(request);
    }

    @Override
    public TableDataInfo<ConverDetailVO> getConvers(ConverPageReq request) {
        if (request == null || request.getPageSize() == null || request.getPageNum() == null
            || StringUtils.isBlank(request.getAppKey()) || StringUtils.isBlank(request.getUserId())
        ) {
            return TableDataInfo.build(new ArrayList<>());
        }
        return converDomainService.getConvers(request);
    }

    @Override
    public ConverItem getConverFromCache(String appKey, String userId, String targetId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(targetId) ) {
            return null;
        }
        return converDomainService.getConverFromCache(appKey, userId, targetId);
    }

}
